package com.nutransit;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.mylibrary.mylibrary.imageloader.ImageLoader;
import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.mylibrary.mylibrary.util.CommonCode;
import com.nutransit.commoncode.Webservice_parametrs;

public class Fragment_User_Request extends Fragment implements Webservice_parametrs
{
	private Context context;
	
	private ListView lv_USER_REQUEST;
	private View rootView;
	private TextView tv_Request_Counter;
	
	private HashMap<String, String> hashMap;
	private JSONArray jsonArray_data;
	public static final int REQUEST_CODE_REQUEST_DETAILS = 123;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		rootView = inflater.inflate(R.layout.activity_user_request, container, false);
		
		init_var();
		
		getData();
		return rootView;
	}
	
	private void init_var()
	{
		context = getActivity();
		
		lv_USER_REQUEST = (ListView) rootView.findViewById(R.id.lv_USER_REQUEST);
		tv_Request_Counter = (TextView) rootView.findViewById(R.id.tv_Request_Counter);
	}
	
	private void getData()
	{
		hashMap = new HashMap<String, String>();
		hashMap.put(TAG_driverid, MainActivity_Nutransit.strUserId);
		
		new AsyncTaskUtility(context, hashMap, URL_GET_RESERVE_REQUEST).getJSONResponse(new AsyncTaskInterface()
		{
			@Override
			public void getJSONObjectFromAsynkTask(JSONObject json)
			{
				try
				{
					if(json!=null && json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
					{
						jsonArray_data = json.getJSONArray(TAG_data);
						if(jsonArray_data!=null)
						{
							tv_Request_Counter.setText(""+jsonArray_data.length());
							lv_USER_REQUEST.setAdapter(new Adapter_Wish_List(context, jsonArray_data));
						}
					}
				}
				catch(JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	public class Adapter_Wish_List extends BaseAdapter
	{
		private JSONArray mlist_main_data;
		private LayoutInflater inflater;
		private ImageLoader imageLoader;

		public Adapter_Wish_List(Context context, JSONArray list)
		{
			this.mlist_main_data = list;
			inflater = LayoutInflater.from(context);
			imageLoader = new ImageLoader(context);
		}

		@Override
		public int getCount()
		{
			return mlist_main_data.length();
		}

		@Override
		public Object getItem(int position)
		{
			try
			{
				return mlist_main_data.get(position);
			}
			catch (JSONException e)
			{
				e.printStackTrace();
				return null;
			}
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		public class ViewHolder
		{
			TextView tv_Price, tv_UserName, tv_Address, tv_Accept;
			ImageView iv_Profile_Pic;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{
			final ViewHolder holder;

			try
			{
				if (convertView == null)
				{
					convertView = inflater.inflate(R.layout.list_row_user_request, parent, false);
					holder = new ViewHolder();
					holder.tv_Price = (TextView) convertView.findViewById(R.id.tv_Price);
					holder.tv_UserName = (TextView) convertView.findViewById(R.id.tv_UserName);
					holder.tv_Address = (TextView) convertView.findViewById(R.id.tv_Address);
					holder.tv_Accept = (TextView) convertView.findViewById(R.id.tv_Accept);
					holder.iv_Profile_Pic = (ImageView) convertView.findViewById(R.id.iv_Profile_Pic);
					convertView.setTag(holder);
				}
				else
				{
					holder = (ViewHolder) convertView.getTag();
				}

				final JSONObject jsonObject = mlist_main_data.getJSONObject(position);
				if(MainActivity_Nutransit.strUserType.equalsIgnoreCase(TAG_van))
				{
					holder.tv_Price.setVisibility(View.INVISIBLE);
					holder.tv_Address.setVisibility(View.INVISIBLE);
					holder.iv_Profile_Pic.setVisibility(View.VISIBLE);
					
					holder.tv_UserName.setText(jsonObject.getString(TAG_username));
					imageLoader.DisplayImage("http://nutransit.net/reservation/uploads/driver/"+jsonObject.getString(TAG_image), holder.iv_Profile_Pic);
				}
				else
				{
					holder.tv_Price.setVisibility(View.VISIBLE);
					holder.tv_Address.setVisibility(View.VISIBLE);
					holder.iv_Profile_Pic.setVisibility(View.INVISIBLE);
					
					holder.tv_Price.setText("$"+CommonCode.formatTo2Digit(jsonObject.getString(TAG_calculatedprice)));
					holder.tv_UserName.setText(jsonObject.getString(TAG_startpoint)+", "+jsonObject.getString(TAG_startcity)+", "+jsonObject.getString(TAG_startstate));
					holder.tv_Address.setText(jsonObject.getString(TAG_endpoint)+", "+jsonObject.getString(TAG_endcity)+", "+jsonObject.getString(TAG_endstate));
				}
				holder.tv_Accept.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View arg0)
					{
						try
						{
							accept_request(jsonObject.getString(TAG_idprimary));
						}
						catch (JSONException e)
						{
							e.printStackTrace();
						}
					}
				});
				convertView.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View arg0)
					{
						Intent intent = new Intent(context, Fragment_Car_Van_Details_Request.class);
						intent.putExtra(TAG_data, jsonObject+"");
						intent.putExtra(TAG_pageid, position);
						startActivityForResult(intent, REQUEST_CODE_REQUEST_DETAILS);
					}
				});
			}
			catch(JSONException e)
			{
				e.printStackTrace();
			}
			return convertView;
		}
	}
	
	protected void accept_request(String strRequestId)
	{
		hashMap = new HashMap<String, String>();
		hashMap.put(TAG_requestid, strRequestId);
		hashMap.put(TAG_flag, "1");
		new AsyncTaskUtility(context, hashMap, URL_ACCEPT_OR_REJECT_REQUEST).getJSONResponse(new AsyncTaskInterface()
		{
			@Override
			public void getJSONObjectFromAsynkTask(JSONObject json)
			{
				try
				{
					if(json!=null)
					{
						if(json.has(TAG_message))
							CommonCode.showMessage(json.getString(TAG_message), context);
						if(json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
						{
							getData();
						}
					}
				}
				catch(JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(data!=null && requestCode==REQUEST_CODE_REQUEST_DETAILS)
		{
			getData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
	}
}
